<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Applicant Extends MY_Controller
{

	private $current_session_user;
	private $auth_to_delete_student;
	private $link_to_view_fees;
	private $view_student_data;
	
	public function __construct()
	{
		parent::__construct();
		//laod acl_config
		$this->config->load('acl_config');
		//get current session user
		$this->current_session_user = strtolower($this->session->userdata('userType'));	
		$this->page_auth->secure_page($this->config->item('secure_page_search'));
		$this->link_to_view_fees = $this->config->item('auth_link_to_view_fees');
		$this->view_student_data = $this->config->item('view_subject_grade');
		$this->auth_to_delete_student = $this->config->item('auth_to_delete_student');
		$this->auth_enrollment_transaction_slip = $this->config->item('enrollment_transaction_slip');
		$this->auth_applicant_status = $this->config->item('applicant_status');
		$this->current_session_user = strtolower($this->session->userdata('userType'));
		$this->auth_to_update_student = $this->config->item('auth_update_student_profile');
		
		$this->load->model('M_search','ms');
		$this->load->model('M_levels','ml');
		$this->load->model('M_applicants','ma');
		$this->load->model('M_fees','mf');
		$this->load->model('M_assessment_fees','maf');
		$this->load->model('M_student');
		$this->load->model('M_entrance_test_results','metr');
		$this->load->model('M_pre_applicants');
		$this->load->model('M_enrollees');
		$this->load->model('M_student_payment_record');
		$this->load->model('M_payee');
		
		$this->_init_pagnate_config();
	}
	
	public function update_status($e_id)
	{
		$this->view_data['system_message'] = $this->_msg();
		
		if($this->input->post('update_applicant')){
			$message = '';
			$message_type = '';
			$enroll_data['schedule_for_testing'] = $this->input->post('schedule_for_testing');
			$enroll_data['guidance_school_records'] = $this->input->post('guidance_school_records');
			$enroll_data['guidance_status'] = $this->input->post('guidance_status');
			$guidance_recommendation = '';
			$profile_data['previous_school'] = $this->input->post('p_a_previous_school');
			$enroll_data['guidance_recommend_level_id'] = $this->input->post('guidance_level_id');
			$enroll_data['e_grade_level'] = $this->input->post('guidance_level_id');
			$enroll_data['guidance_approved'] = $this->input->post('guidance_approved');
			$e_profile_id = $this->input->post('e_profile_id');

			if($this->input->post('fee')){
				$c_spr_id = $this->M_student_payment_record->current_payment_record_id($e_id);
				$limiter = 0;
				foreach($this->input->post('fee') as $key)
				{
					$limiter++;
				}
				
				$counter =0;
				foreach($this->input->post('fee') as $key => $value)
				{
					foreach($value as $rate => $name)
					{
						$data[$counter]['sef_fee_name'] = $name;
						$data[$counter]['sef_fee_rate'] = $rate;
						$data[$counter]['sef_enrollment_id'] = $e_id;
						$data[$counter]['sef_gperiod_id'] = $this->current_grading_period;
						$data[$counter]['sef_schoolyear_id'] = $this->current_school_year;
						$data[$counter]['sef_created'] =NOW;
						$data[$counter]['is_other_fee'] = 1;
							
						if($c_spr_id !== NULL)
						{
							$for_pb[$counter]['spb_fee_name'] =$name;
							$for_pb[$counter]['spb_remaining_balance'] = $rate;
							$for_pb[$counter]['spb_fee_rate'] =$rate;
							$for_pb[$counter]['spb_transacted_by'] =$this->session->userdata('userid');
							$for_pb[$counter]['spb_spr_id'] =$c_spr_id;
							$for_pb[$counter]['spb_created'] =NOW;
							$for_pb[$counter]['spb_stud_enrollment_id'] = $e_id_post;
							$for_pb[$counter]['spb_is_other_fee'] = 1;
						}else{
							$for_pb = FALSE;
						}

						$counter++;
						$guidance_recommendation =  $guidance_recommendation.$name;
						if($counter < $limiter){
							$guidance_recommendation = $guidance_recommendation.', ';
						}
					}
				}
				
				if($this->M_enrollees->cashier_add_new_fees($data,$for_pb) == TRUE){
					$payment_added = 'yes';
				}else{
					$payment_added = 'no';
				}
			}
			
			if($this->input->post('fee')){
				if($this->input->post('guidance_recommendation')){
					$enroll_data['guidance_recommendation'] = $guidance_recommendation.', '.$this->input->post('guidance_recommendation');
				}else{
					$enroll_data['guidance_recommendation'] = $guidance_recommendation;
				}
			}else{
				$enroll_data['guidance_recommendation'] = $this->input->post('guidance_recommendation');
			}

			if($this->input->post('p_a_moved') == 'yes'){
				$enroll_data['moved_to'] = 'yes';
				$this->db->update('enrollments', $enroll_data, array("e_id" => $e_id));
				if($this->db->affected_rows() >= 1):
					$this->_recompute_fee($e_id);
					if($payment_added == 'yes'){
						//$this->view_data['system_message'] = $this->_msg('s','Applicant record updated and fees successfully added.','applicant/update_status/'.$e_id);
						$message_type = 's';
						$message = 'Applicant record updated and fees successfully added.';
					}elseif($payment_added == 'no'){
						//$this->view_data['system_message'] = $this->_msg('s','Applicant record updated. Fess not successfully added. Insert the fees manually.','applicant/update_status/'.$e_id);
						$message_type = 's';
						$message = 'Applicant record updated. Fess not successfully added. Insert the fees manually.';
					}else{
						//$this->view_data['system_message'] = $this->_msg('s','Applicant record updated.','applicant/update_status/'.$e_id);
						$message_type = 's';
						$message = 'Applicant record updated.';
					}
				else:
					//$this->view_data['system_message'] = $this->_msg('e','Applicant record not updated or no new data to be saved.','applicant/update_status/'.$e_id);
					$message_type = 'e';
					$message = 'Applicant record not updated or no new data to be saved.';
				endif;
			}else{
				$this->db->update('enrollments', $enroll_data, array("e_id" => $e_id));
				if($this->db->affected_rows() >= 1):
					$this->_recompute_fee($e_id);
					if($payment_added == 'yes'){
						//$this->view_data['system_message'] = $this->_msg('s','Applicant record updated and fees successfully added.','applicant/update_status/'.$e_id);
						$message_type = 's';
						$message = 'Applicant record updated and fees successfully added.';
					}elseif($payment_added == 'no'){
						//$this->view_data['system_message'] = $this->_msg('s','Applicant record updated. Fess not successfully added. Insert the fees manually.','applicant/update_status/'.$e_id);
						$message_type = 's';
						$message = 'Applicant record updated. Fess not successfully added. Insert the fees manually.';
					}else{
						//$this->view_data['system_message'] = $this->_msg('s','Applicant record updated.','applicant/update_status/'.$e_id);
						$message_type = 's';
						$message = 'Applicant record updated.';
					}
				else:
					//$this->view_data['system_message'] = $this->_msg('e','Applicant record not updated or no new data to be saved.','applicant/update_status/'.$e_id);
					$message_type = 'e';
					$message = 'Applicant record not updated or no new data to be saved.';
				endif;
			}
			
			if($this->input->post('p_a_previous_school')!=''){
				$this->db->update('profiles', $profile_data, array("profile_id" => $e_profile_id));
				if($this->db->affected_rows() >= 1){
					//$this->view_data['system_message'] = $this->_msg('s','Applicant record updated.','applicant/update_status/'.$e_id);
					$message_type = 's';
					$message = 'Applicant record updated.';
				}else{
					//$this->view_data['system_message'] = $this->_msg('e','Applicant record not updated.','applicant/update_status/'.$e_id);
					$message_type = 'e';
					$message = 'Applicant record not updated.';
				}
			}
			
			if($message)
			{
				$this->view_data['system_message'] = $this->_msg($message_type,$message,'applicant/update_status/'.$e_id);
			}
		}
		
		/*
		 * Updates student entrance test result
		 */
		if($this->input->post('insert_entrance_test_result')){
			if(in_array($this->current_session_user,$this->auth_to_update_student)):
				$v = (object)$this->input->post();
				$profile_data['e_id'] = $v->e_id;
				$profile_data['reading_number_right'] = $v->reading_number_right;
				$profile_data['reading_scaled_score'] = $v->reading_scaled_score;
				$profile_data['reading_grade_equivalent'] = $v->reading_grade_equivalent;
				$profile_data['reading_percentile_rank'] = $v->reading_percentile_rank;
				$profile_data['reading_stanine'] = $v->reading_stanine;
				$profile_data['reading_instructional_level'] = $v->reading_instructional_level;
				$profile_data['math_number_right'] = $v->math_number_right;
				$profile_data['math_scaled_score'] = $v->math_scaled_score;
				$profile_data['math_grade_equivalent'] = $v->math_grade_equivalent;
				$profile_data['math_percentile_rank'] = $v->math_percentile_rank;
				$profile_data['math_stanine'] = $v->reading_stanine;
				$profile_data['language_number_right'] = $v->language_number_right;
				$profile_data['language_scaled_score'] = $v->language_scaled_score;
				$profile_data['language_grade_equivalent'] = $v->language_grade_equivalent;
				$profile_data['language_percentile_rank'] = $v->language_percentile_rank;
				$profile_data['language_stanine'] = $v->language_stanine;
				$profile_data['basic_battery_number_right'] = $v->basic_battery_number_right;
				$profile_data['basic_battery_scaled_score'] = $v->basic_battery_scaled_score;
				$profile_data['basic_battery_grade_equivalent'] = $v->basic_battery_grade_equivalent;
				$profile_data['basic_battery_percentile_rank'] = $v->basic_battery_percentile_rank;
				$profile_data['basic_battery_stanine'] = $v->basic_battery_stanine;
				
				
				if($this->metr->insert_entrance_test_result($profile_data)):
					$this->_msg('s','Information about Applicant Entrance Test Result was successfully Updated','applicant/update_status/'.$v->e_id);
				else:
					$this->_msg('e','An error occured while trying to update Entance Test Results.','applicant/update_status/'.$v->e_id);
				endif;
			else:
				$this->view_data['system_message'] = $this->_msg('n','You have not been granted Privileges to update This data');
			endif;
		}
		
		if($this->input->post('update_entrance_test_result')){
			if(in_array($this->current_session_user,$this->auth_to_update_student)):
				$v = (object)$this->input->post();
				//$profile_data['e_id'] = $v->e_id;
				$profile_data['reading_number_right'] = $v->reading_number_right;
				$profile_data['reading_scaled_score'] = $v->reading_scaled_score;
				$profile_data['reading_grade_equivalent'] = $v->reading_grade_equivalent;
				$profile_data['reading_percentile_rank'] = $v->reading_percentile_rank;
				$profile_data['reading_stanine'] = $v->reading_stanine;
				$profile_data['reading_instructional_level'] = $v->reading_instructional_level;
				$profile_data['math_number_right'] = $v->math_number_right;
				$profile_data['math_scaled_score'] = $v->math_scaled_score;
				$profile_data['math_grade_equivalent'] = $v->math_grade_equivalent;
				$profile_data['math_percentile_rank'] = $v->math_percentile_rank;
				$profile_data['math_stanine'] = $v->reading_stanine;
				$profile_data['language_number_right'] = $v->language_number_right;
				$profile_data['language_scaled_score'] = $v->language_scaled_score;
				$profile_data['language_grade_equivalent'] = $v->language_grade_equivalent;
				$profile_data['language_percentile_rank'] = $v->language_percentile_rank;
				$profile_data['language_stanine'] = $v->language_stanine;
				$profile_data['basic_battery_number_right'] = $v->basic_battery_number_right;
				$profile_data['basic_battery_scaled_score'] = $v->basic_battery_scaled_score;
				$profile_data['basic_battery_grade_equivalent'] = $v->basic_battery_grade_equivalent;
				$profile_data['basic_battery_percentile_rank'] = $v->basic_battery_percentile_rank;
				$profile_data['basic_battery_stanine'] = $v->basic_battery_stanine;
				
				
				if($this->metr->update_entrance_test_result($v->e_id,$profile_data)):
					$this->_msg('s','Information about Applicant Entrance Test Result was successfully Updated','applicant/update_status/'.$v->e_id);
				else:
					$this->_msg('e','An error occured while trying to update Entrance Test Result.','applicant/update_status/'.$v->e_id);
				endif;
			else:
				$this->view_data['system_message'] = $this->_msg('n','You have not been granted Privileges to update This data');
			endif;
		}
		
		$this->view_data['levels'] = $this->ml->get_all_levels_array($choose = FALSE);
		$this->view_data['applicants'] = $applicants = $this->ma->get_applicant($e_id);
		$this->view_data['student'] = $this->M_student->get_student($e_id);
		//$this->view_data['applicant'] = $applicant = $this->M_pre_applicants->get_applicants($e_id);
		$this->view_data['entrance_test_result'] = $this->metr->get_entrance_test_result($e_id);
		$this->view_data['payee'] = $this->ma->get_payee($applicants->e_profile_id);
		$this->view_data['af'] = $this->ma->get_af($e_id);
		$optional_fees = $this->M_enrollees->get_available_fees2($e_id);
		if($optional_fees == FALSE){
			$optional_fees = $this->M_enrollees->get_optional();
		}
		$this->view_data['optional_fees'] = $optional_fees;
	}
	
	public function update_interview($e_id){
		$this->view_data['system_message'] = $this->_msg();
		
		$this->view_data['levels'] = $this->ml->get_all_levels_array($choose = FALSE);
		$this->view_data['applicants'] = $this->ma->get_applicant($e_id);
		
		if($this->input->post('update_applicant')){
			//$input['e_id'] = $e_id;
			$input['e_grade_level'] = $this->input->post('level_id');
			$input['vice_principal_interview'] = $this->input->post('vice_principal_interview');

			$this->db->update('enrollments', $input, array("e_id" => $e_id));
			if($this->db->affected_rows() >= 1)
			{
				$this->view_data['system_message'] = $this->_msg('s','Applicant enrollment record updated.','applicant/update_interview/'.$e_id);
			}else{
				$this->view_data['system_message'] = $this->_msg('e','Applicant enrollment record not updated. Please try again.','applicant/update_interview/'.$e_id);
			}
		}
		
	}
	
	public function assessment_fee($e_id){
		$this->view_data['applicants'] = $applicants = $this->ma->get_applicant($e_id);
		$this->view_data['feedata'] = $fee_data = $this->mf->get_assessment_fees_by_grade_level();
		$this->view_data['payee'] = $payee = $this->M_payee->get_payee($applicants->e_profile_id);
		$this->view_data['system_message'] = $this->_msg();

		//Assessment Fee payment process
		if($this->input->post('update_fee')){
				
			$input['user_trans_id'] = $input_spr['spr_user_trans_id'] = $input_spb['spb_transacted_by'] = $this->session->userdata('userid');
			$input['user_trans_name'] = $input_spr['spr_user_trans_name'] = $this->session->userdata('username');
			$input['enrollment_id'] = $input_spr['spr_enrollment_id'] = $input_spb['spb_stud_enrollment_id'] = $e_id;
			//$input['payee'] = $this->input->post('payee');
			$input['relation'] = $this->input->post('relation');
			$input['or_no'] = $input_spr['spr_or_no'] = $this->input->post('spr_or_no');
			$dater = $this->input->post('date_of_payment');
			if($dater==''){	$dater = date('m/d/Y');	}
			$input['payment_date'] = $input_spr['spr_payment_date'] =  date('Y-m-d',strtotime($dater));
			$input['cheque_no'] = $input_spr['cheque_no'] = $this->input->post('cheque_no');
			$input['credit_card_no'] = $input_spr['credit_card_no'] = $this->input->post('credit_card_no');
			$input['credit_card_name'] = $input_spr['credit_card_name'] = $this->input->post('credit_card_name');
			$input['payment_mode'] = $input_spr['spr_mode_of_payment'] = $this->input->post('payment_mode')==''?"Cash":$this->input->post('payment_mode');
			$input['created'] = $input_spr['spr_created'] = $input_spb['spb_created'] = NOW;
			$input_spr['spr_remarks'] = $input_spb['spb_fee_name'] = $fee_data->fee_name;
			$input_spr['spr_ammt_paid'] = $input_spb['spb_fee_rate'] = $input_spb['spb_amount_paid'] = $fee_data->fee_rate;
			$input_spr['spr_schoolyear_id'] = $input_spb['spb_sy_id'] = $this->current_school_year;
			$input_spb['spb_is_other_fee'] = 1;
			$input_spr['spr_for_the_month_of'] = 'Application';
			
			$enrolldata['schedule_for_testing'] = $this->input->post('schedule_for_testing');
				
			if($this->input->post('p_a_charge_to') == 'yes'){	
				$enrolldata['charge_to'] = $this->input->post('p_a_charge_to');
				if($this->ma->update_applicant_enrollment($e_id,$enrolldata) == TRUE){
					$this->authlink->clear_authlink();
					$this->view_data['system_message'] = $this->_msg('s','Charge To has been applied to the applicant.','search/applicants');
				}else{
					$this->authlink->clear_authlink();
					$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/inquiries');
				}
			}else{
				if($payee){
					$student_profile_id = $this->input->post('student_profile_id');
					$data['firstname'] = $this->input->post('firstname');
					$data['middlename'] = $this->input->post('middlename');
					$data['lastname'] = $this->input->post('lastname');
					$data['name_ext'] = $this->input->post('name_ext');
					$data['relation'] = $this->input->post('relation');
					if($this->M_payee->update_applicant_payee($student_profile_id,$data) == TRUE){
						$sql = "SELECT f.fee_name,f.fee_rate
										FROM fees f 
										WHERE f.is_assess_fee = 1
										AND f.ay_id = ?
										AND f.fee_stat = 'active'
										";
						$query = $this->db->query($sql,array($this->current_school_year));
						$data = $query->num_rows() >= 1 ? $query->result() : FALSE;
						
						if(!empty($data)){
						//format assessment fees for entry
						$count = 0;
						foreach($data as $fees){
							$s_e_fees[$count]['sef_enrollment_id'] =$e_id;
							$s_e_fees[$count]['sef_fee_name'] = $fees->fee_name;
							$s_e_fees[$count]['sef_fee_rate'] = $fees->fee_rate;
							$s_e_fees[$count]['sef_gperiod_id'] = $this->current_grading_period;
							$s_e_fees[$count]['sef_schoolyear_id'] = $this->current_school_year;
							$s_e_fees[$count]['sef_created'] = NOW;
							$s_e_fees[$count]['is_other_fee'] = 1;
							$count++;
						}
						}
						$this->db->insert_batch('student_enrollment_fees',$s_e_fees);

						if($this->ma->insert_applicant_assessment_fee($e_id,$input) == TRUE){
							$this->db->insert('student_payment_records', $input_spr); 
							$spr_id = $this->db->insert_id();
							$input_spb['spb_spr_id'] = $spr_id;
							$sql = "SELECT sef_id
										FROM student_enrollment_fees 
										WHERE is_other_fee = 1
										AND sef_enrollment_id = ?
										AND sef_fee_name = ?
										";
							$query = $this->db->query($sql,array($e_id,$fee_data->fee_name));
							$data = $query->num_rows() >= 1 ? $query->row() : FALSE;
							$input_spb['spb_sef_id'] = $data->sef_id;
							$this->db->insert('student_payment_breakdown', $input_spb);
							
							$enrolldata['af_paid'] = 'yes';
							if($this->ma->update_applicant_enrollment($e_id,$enrolldata) == TRUE){
								$this->authlink->clear_authlink();
								$this->view_data['system_message'] = $this->_msg('s','Payment transaction has been recorded. You can print the receipt found on the more button.','search/applicants');
							}else{
								$this->authlink->clear_authlink();
								$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/inquiries');
							}
						}else{
							$this->authlink->clear_authlink();
							$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/inquiries');
						}
					}else{
						$this->authlink->clear_authlink();
						$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/inquiries');
					}
				}else{
					$data['student_profile_id'] = $this->input->post('student_profile_id');
					$data['firstname'] = $this->input->post('firstname');
					$data['middlename'] = $this->input->post('middlename');
					$data['lastname'] = $this->input->post('lastname');
					$data['name_ext'] = $this->input->post('name_ext');
					$data['relation'] = $this->input->post('relation');
					if($this->ma->insert_applicant_payee($data) == TRUE){
						$sql = "SELECT f.fee_name,f.fee_rate
										FROM fees f 
										WHERE f.is_assess_fee = 1
										AND f.ay_id = ?
										AND f.fee_stat = 'active'
										";
						$query = $this->db->query($sql,array($this->current_school_year));
						$data = $query->num_rows() >= 1 ? $query->result() : FALSE;

						if(!empty($data)){
						//format assessment fees for entry
						$count = 0;
						foreach($data as $fees){
							$s_e_fees[$count]['sef_enrollment_id'] =$e_id;
							$s_e_fees[$count]['sef_fee_name'] = $fees->fee_name;
							$s_e_fees[$count]['sef_fee_rate'] = $fees->fee_rate;
							$s_e_fees[$count]['sef_gperiod_id'] = $this->current_grading_period;
							$s_e_fees[$count]['sef_schoolyear_id'] = $this->current_school_year;
							$s_e_fees[$count]['sef_created'] = NOW;
							$s_e_fees[$count]['is_other_fee'] = 1;
							$count++;
						}
						}
						$this->db->insert_batch('student_enrollment_fees',$s_e_fees);

						if($this->ma->insert_applicant_assessment_fee($e_id,$input) == TRUE){
							$this->db->insert('student_payment_records', $input_spr);
							$spr_id = $this->db->insert_id(); 
							$input_spb['spb_spr_id'] = $spr_id;
							$sql = "SELECT sef_id
										FROM student_enrollment_fees 
										WHERE is_other_fee = 1
										AND sef_enrollment_id = ?
										AND sef_fee_name = ?
										";
							$query = $this->db->query($sql,array($e_id,$fee_data->fee_name));
							$data = $query->num_rows() >= 1 ? $query->row() : FALSE;
							$input_spb['spb_sef_id'] = $data->sef_id;
							$this->db->insert('student_payment_breakdown', $input_spb);
							$enrolldata['af_paid'] = 'yes';
							if($this->ma->update_applicant_enrollment($e_id,$enrolldata) == TRUE){
								$this->authlink->clear_authlink();
								$this->view_data['system_message'] = $this->_msg('s','Payment transaction has been recorded. You can print the receipt found on the more button.','search/applicants');
							}else{
								$this->authlink->clear_authlink();
								$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/inquiries');
							}
						}else{
							$this->authlink->clear_authlink();
							$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/inquiries');
						}
					}else{
						$this->authlink->clear_authlink();
						$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/inquiries');
					}
				}
			}	
		}	
	}

	public function assessment_fee_update($e_id)
	{
		$this->view_data['applicants'] = $applicants = $this->ma->get_applicant($e_id);
		$this->view_data['feedata'] = $this->mf->get_assessment_fees_by_grade_level();
		$this->view_data['payee'] = $payee = $this->M_payee->get_payee($applicants->e_profile_id);
		$this->view_data['system_message'] = $this->_msg();

		//Assessment Fee payment process
		if($this->input->post('update_fee')){
			$input['user_trans_id'] = $input_spr['spr_user_trans_id'] = $input_spb['spb_transacted_by'] = $this->session->userdata('userid');
			$input['user_trans_name'] = $input_spr['spr_user_trans_name'] = $this->session->userdata('username');
			$input['enrollment_id'] = $input_spr['spr_enrollment_id'] = $input_spb['spb_stud_enrollment_id'] = $e_id;
			//$input['payee'] = $this->input->post('payee');
			$input['relation'] = $this->input->post('relation');
			$input['or_no'] = $input_spr['spr_or_no'] = $this->input->post('spr_or_no');
			$dater = $this->input->post('date_of_payment');
			if($dater==''){	$dater = date('m/d/Y');	}
			$input['payment_date'] = $input_spr['spr_payment_date'] = date('Y-m-d',strtotime($dater));
			$input['cheque_no'] = $input_spr['cheque_no'] = $this->input->post('cheque_no');
			$input['credit_card_no'] = $input_spr['credit_card_no'] = $this->input->post('credit_card_no');
			$input['credit_card_name'] = $input_spr['credit_card_name'] = $this->input->post('credit_card_name');
			$input['payment_mode'] = $input_spr['spr_mode_of_payment'] = $this->input->post('payment_mode');
			$input['created'] = $input_spr['spr_created'] = $input_spb['spb_created'] = NOW;
			$input_spr['spr_remarks'] = $input_spb['spb_fee_name'] = $fee_data->fee_name;
			$input_spr['spr_ammt_paid'] = $input_spb['spb_fee_rate'] = $input_spb['spb_amount_paid'] = $fee_data->fee_rate;
			$input_spr['spr_schoolyear_id'] = $input_spb['spb_sy_id'] = $this->current_school_year;
			$input_spb['spb_is_other_fee'] = 1;
			$input_spr['spr_for_the_month_of'] = 'Application';
			
			$enrolldata['schedule_for_testing'] = $this->input->post('schedule_for_testing');
			if($payee){
				$student_profile_id = $this->input->post('student_profile_id');
				$data['firstname'] = $this->input->post('firstname');
				$data['middlename'] = $this->input->post('middlename');
				$data['lastname'] = $this->input->post('lastname');
				$data['name_ext'] = $this->input->post('name_ext');
				$data['relation'] = $this->input->post('relation');
				if($this->M_payee->update_applicant_payee($student_profile_id,$data) == TRUE){
				$sql = "SELECT f.fee_name,f.fee_rate
									FROM fees f 
									WHERE f.is_assess_fee = 1
									AND f.ay_id = ?
									AND f.fee_stat = 'active'
									";
					$query = $this->db->query($sql,array($this->current_school_year));
					$data = $query->num_rows() >= 1 ? $query->result() : FALSE;
					
					if(!empty($data)){
					//format assessment fees for entry
					$count = 0;
					foreach($data as $fees){
						$s_e_fees[$count]['sef_enrollment_id'] =$e_id;
						$s_e_fees[$count]['sef_fee_name'] = $fees->fee_name;
						$s_e_fees[$count]['sef_fee_rate'] = $fees->fee_rate;
						$s_e_fees[$count]['sef_gperiod_id'] = $this->current_grading_period;
						$s_e_fees[$count]['sef_schoolyear_id'] = $this->current_school_year;
						$s_e_fees[$count]['sef_created'] = NOW;
						$s_e_fees[$count]['is_other_fee'] = 1;
						$count++;
					}
					}
					$this->db->insert_batch('student_enrollment_fees',$s_e_fees);

					if($this->ma->insert_applicant_assessment_fee($e_id,$input) == TRUE){
						$this->db->insert('student_payment_records', $input_spr); 
						$spr_id = $this->db->insert_id(); 
						$input_spb['spb_spr_id'] = $spr_id;
						$sql = "SELECT sef_id
									FROM student_enrollment_fees 
									WHERE is_other_fee = 1
									AND sef_enrollment_id = ?
									AND sef_fee_name = ?
									";
						$query = $this->db->query($sql,array($e_id,$fee_data->fee_name));
						$data = $query->num_rows() >= 1 ? $query->row() : FALSE;
						$input_spb['spb_sef_id'] = $data->sef_id;
						$this->db->insert('student_payment_breakdown', $input_spb);
						$enrolldata['af_paid'] = 'yes';
						$enrolldata['charge_to'] = 'no';
						if($this->ma->update_applicant_enrollment($e_id,$enrolldata) == TRUE){
							$this->authlink->clear_authlink();
							$this->view_data['system_message'] = $this->_msg('s','Payment transaction has been recorded. You can print the receipt found on the more button.','search/applicants');
						}else{
							$this->authlink->clear_authlink();
							$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/applicants');
						}
					}else{
						$this->authlink->clear_authlink();
						$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/applicants');
					}
				}else{
					$this->authlink->clear_authlink();
					$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/applicants');
				}
			}else{
				$data['student_profile_id'] = $this->input->post('student_profile_id');
				$data['firstname'] = $this->input->post('firstname');
				$data['middlename'] = $this->input->post('middlename');
				$data['lastname'] = $this->input->post('lastname');
				$data['name_ext'] = $this->input->post('name_ext');
				$data['relation'] = $this->input->post('relation');
				if($this->ma->insert_applicant_payee($data) == TRUE){
				$sql = "SELECT f.fee_name,f.fee_rate
									FROM fees f 
									WHERE f.is_assess_fee = 1
									AND f.ay_id = ?
									AND f.fee_stat = 'active'
									";
					$query = $this->db->query($sql,array($this->current_school_year));
					$data = $query->num_rows() >= 1 ? $query->result() : FALSE;
					
					if(!empty($data)){
					//format assessment fees for entry
					$count = 0;
					foreach($data as $fees){
						$s_e_fees[$count]['sef_enrollment_id'] =$e_id;
						$s_e_fees[$count]['sef_fee_name'] = $fees->fee_name;
						$s_e_fees[$count]['sef_fee_rate'] = $fees->fee_rate;
						$s_e_fees[$count]['sef_gperiod_id'] = $this->current_grading_period;
						$s_e_fees[$count]['sef_schoolyear_id'] = $this->current_school_year;
						$s_e_fees[$count]['sef_created'] = NOW;
						$s_e_fees[$count]['is_other_fee'] = 1;
						$count++;
					}
					}
					$this->db->insert_batch('student_enrollment_fees',$s_e_fees);

					if($this->ma->insert_applicant_assessment_fee($e_id,$input) == TRUE){
						$this->db->insert('student_payment_records', $input_spr); 
						$spr_id = $this->db->insert_id(); 
						$input_spb['spb_spr_id'] = $spr_id;
						$sql = "SELECT sef_id
									FROM student_enrollment_fees 
									WHERE is_other_fee = 1
									AND sef_enrollment_id = ?
									AND sef_fee_name = ?
									";
						$query = $this->db->query($sql,array($e_id,$fee_data->fee_name));
						$data = $query->num_rows() >= 1 ? $query->row() : FALSE;
						$input_spb['spb_sef_id'] = $data->sef_id;
						$this->db->insert('student_payment_breakdown', $input_spb);
						$enrolldata['af_paid'] = 'yes';
						$enrolldata['charge_to'] = 'no';
						if($this->ma->update_applicant_enrollment($e_id,$enrolldata) == TRUE){
							$this->authlink->clear_authlink();
							$this->view_data['system_message'] = $this->_msg('s','Payment transaction has been recorded. You can print the receipt found on the more button.','search/applicants');
						}else{
							$this->authlink->clear_authlink();
							$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/applicants');
						}
					}else{
						$this->authlink->clear_authlink();
						$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/applicants');
					}
				}else{
					$this->authlink->clear_authlink();
					$this->view_data['system_message'] = $this->_msg('e','System Error. Please try again.','search/applicants');
				}
			}
		}	
	}
	
	public function applicant_report()
	{
		$this->view_data['system_message'] = $this->_msg();
		
		//$this->view_data['applicants'] = $this->ma->get_applicant();
		$this->view_data['applicants'] = $this->M_pre_applicants->get_guidance_approval();
	}

	/*+----------------
	  | Recomputes Student Fees
	  +----------------
	*/
	private function _recompute_fee($e_id)
	{
		//delete student enrollment fees by enrollment id because we will insert a new one.
		// this is better than updating the old fees, much faster

		$this->db->where('sef_enrollment_id',$e_id)
						 ->where('sef_schoolyear_id',$this->current_school_year)
						 ->where('is_elective_fee !=', 1)
						 ->where('is_other_fee !=', 1)
						 ->delete('student_enrollment_fees');
		
		//generate student fees
		$this->load->model('M_level_fees','mlf');
			
		$res = $this->mlf->generate_student_fees($e_id);
		if($res->stat == 'true')
		{
			//recalculate tuition and miscelaneous fee according to payment plan
			$this->load->model('M_payment_mode','mpm');
			$this->load->model('M_payment_mode_value','mpmv');

			$selected_payment_mode = $this->M_enrollees->get_student_payment_mode($e_id);
			$enrollment_data = $this->M_enrollees->get_enrollee_prof($e_id);
			$payment_plan_id = $this->mpm->get_payment_plan_id($selected_payment_mode->payment_mode);
			
			$tuit = $this->mpmv->get_payment_plan_tuit($enrollment_data->level_id,$payment_plan_id->id);
			$matr = $this->mpmv->get_payment_plan_matr($enrollment_data->level_id,$payment_plan_id->id);

			if(floatval($matr->payment_plan_matr)!=0.00 && floatval($tuit->payment_plan_tuit)!=0.00){
				if(strtolower($selected_payment_mode->payment_mode=='cash basis')){
					$newtuit = floatval ($tuit->payment_plan_tuit) * 1.00;
					$newmatr = floatval ($matr->payment_plan_matr) * 1.00;
				}
				if(strtolower($selected_payment_mode->payment_mode=='2- payment plan')){
					$newtuit = floatval ($tuit->payment_plan_tuit) * 2.00;
					$newmatr = floatval ($matr->payment_plan_matr) * 2.00;
				}
				if(strtolower($selected_payment_mode->payment_mode=='4- payment plan')){
					$newtuit = floatval ($tuit->payment_plan_tuit) * 4.00;
					$newmatr = floatval ($matr->payment_plan_matr) * 4.00;
				}
				if(strtolower($selected_payment_mode->payment_mode=='9- payment plan')){
					$newtuit = floatval ($tuit->payment_plan_tuit) * 9.00;
					$newmatr = floatval ($matr->payment_plan_matr) * 9.00;
				}

				//update tuition and matriculation
				$sql = "UPDATE student_enrollment_fees 
								SET sef_fee_rate = ?
								WHERE sef_enrollment_id = ? AND 
								is_tuition_fee = 1";
				$this->db->query($sql,array($newtuit,$e_id));

				$sql = "UPDATE student_enrollment_fees 
								SET sef_fee_rate = ?
								WHERE sef_enrollment_id = ? AND 
								is_cca_fee = 1";
				$this->db->query($sql,array($newmatr,$e_id));
			}
			//return true
			$this->_msg('s',$res->log,'fees/view_fees/'.$e_id);
		}else{
			//return false
			$this->_msg('e',$res->log,'fees/view_fees/'.$e_id);
		}


		return TRUE;

		//process scholarships
		/*
		$res = $this->M_fees->recompute_student_fees($e_id);

		// vd($res);
		if($res)
		{
			$this->load->model('M_scholarships','ms');
			//get student payment scheme
			$type = $this->M_enrollees->get_student_payment_mode($e_id);
			//recompute the payment plan
			$recompute_payment_plan = $this->M_fees->recompute_payment_plan($e_id);
			//recalculate paid fees
			$add_payment_to_fees = $this->M_fees->recalculate_paid_fees($e_id,$type);
			// add and compute scholarships
			$this->ms->readd_scholarships($e_id);
			// add and compute deductions
			$this->ms->readd_deductions($e_id);
			
			if($recompute_payment_plan)
			{
				if($add_payment_to_fees)
				{
					return TRUE;
				}else{
					return FALSE;
				}
			}else{
				return FALSE;
			}
		}else{
			return FALSE;
		}*/
	}
}